console.log(123);

setTimeout(() => {
    console.log('1  second,show');
}, 1000);

let count = 1;
let time = setInterval(() => {
    count++;
    console.log(count);
    if (count > 5) {
        myEmitter.emit('someEvenets', 'xixi');
        clearInterval(time);
    }
}, 1000);

const obj = require('./count');
const events = require('events');
const util = require('util');
let myEmitter = new events.EventEmitter();
myEmitter.on('someEvenets', function(arg) {
    console.log('触发自定义函数', arg);
});

let Person = function(name) {
    events.call(this);
    this.name = name;
};

util.inherits(Person, events.EventEmitter);

let tom = new Person('tom');
let json = new Person('json');
let person = [tom, json];
for (const iterator of person) {
    iterator.on('speak', function(message) {
        console.log(this.name + ' say:' + message);
    });
}

json.emit('speak', 'hi');
tom.emit('speak', 'lla');

const fs = require('fs');

fs.readFile('count.js', { encoding: 'utf-8' }, function(err, data) {
    if (err) return;
    return;
});
let readfilePromise = function(file) {
    const p1 = new Promise(function(resolve, reject) {
        fs.readFile(file, { encoding: 'utf-8' }, function(err, data) {
            if (err) reject(err);
            resolve(data);
        });
    });
    return p1;
};
readfilePromise('count.js1')
    .then(function(data) {
        //console.log(data);
    })
    .catch(function(err) {
        console.log(err);
    });
fs.access('count.js', fs.constants.F_OK, (err, data) => {
    if (err) {
        console.log('没有此文件');
        return;
    }
    console.log('存在此文件');
});

let readStream = fs.createReadStream(__dirname + '/count.js');
let writeStream = fs.createWriteStream(__dirname + '/write.js');
let data = '';
readStream.on('data', function(chunk) {
    data += chunk;
    writeStream.write(chunk);
});
readStream.on('end', function(chunk) {
    console.log(data);
});


